<?php

class Adviser extends Controller {

    public function __construct() {
        parent::Controller();
        if (!$this->session->userdata('isAuthenticated')) {
            redirect(base_url().'admin.php?c=auth&m=index');
        }
        $this->load->model('advisermodel', 'adviser');
        $this->load->model('photomodel', 'photo');
    }

    public function index() {
        $this->load->library('pagination');

        $limit = 10;
        $total = $this->adviser->a_count();
        $offset = intval($this->input->get('per_page'));

        $config['base_url'] = base_url() . 'admin.php/?c=adviser&m=index';
        $config['total_rows'] = $total;
        $config['per_page'] = $limit;
        $this->pagination->initialize($config);

        $data['title'] = '顾问单位';
        $data['advisers'] = $this->adviser->a_list($limit, $offset);
        $data['page_links'] = $this->pagination->create_links();
        $this->load->view('adviser/index', $data);
    }

    public function create() {
        $this->load->helper('form');
        $data['title'] = '添加顾问公司';
        $data['photos'] = $this->getPhotos();
        $this->load->view('adviser/create', $data);
    }

    public function add() {
        $this->load->library('form_validation');
		
		$this->form_validation->set_rules('name', '单位名称', 'required');
		$this->form_validation->set_rules('url', '单位网址', 'required');
		$this->form_validation->set_rules('content', '单位简介', 'required');
		
        if ($this->form_validation->run() == false) {
            $data['title'] = '添加顾问公司';
            $data['photos'] = $this->getPhotos();
            $this->load->view('adviser/create', $data);
        } else {
            $adviser['logo'] = $this->input->post('logo');
            $adviser['url'] = $this->input->post('url');
            $adviser['name'] = $this->input->post('name');
            $adviser['content'] = $this->input->post('content');
            if ($this->db->insert('adviser', $adviser)) {
                $this->session->set_flashdata('notice', '添加成功！');
            } else {
                $this->session->set_flashdata('error', '添加失败，数据库错误！');
            }
            redirect(base_url().'admin.php?c=adviser&m=index');
        }
    }

    public function edit() {
        $this->load->helper('form');
        $id = $this->getId();
        if ($entry = $this->adviser->find($id)) {
            $data['title'] = '编辑' . $entry->name . '的资料';
            $data['photos'] = $this->getPhotos();
            $data['entry'] = $entry;
            $this->load->view('adviser/edit', $data);
        } else {
            show_404();
        }
    }

    public function update() {
        $this->load->library('form_validation');
        $id = intval($this->input->post('id'));
		
		$this->form_validation->set_rules('name', '单位名称', 'required');
		$this->form_validation->set_rules('url', '单位网址', 'required');
		$this->form_validation->set_rules('content', '单位简介', 'required');
		
        if ($this->adviser->find($id)) {
            if ($this->form_validation->run() == false) {
                $adviser = $this->adviser->find($id);
                $data['title'] = '编辑' . $adviser->name . '的资料';
                $data['photos'] = $this->getPhotos();
                $data['entry'] = $adviser;
                $this->load->view('adviser/edit', $data);
            } else {
                $adviser['logo'] = $this->input->post('logo');
                $adviser['url'] = $this->input->post('url');
                $adviser['name'] = $this->input->post('name');
                $adviser['content'] = $this->input->post('content');
                if ($this->db->update('adviser', $adviser, array('id' => $id))) {
                    $this->session->set_flashdata('notice', '修改成功！');
                } else {
                    $this->session->set_flashdata('error', '修改失败，数据库错误！');
                }
                redirect(base_url().'admin.php?c=adviser&m=index');
            }
        } else {
            show_404();
        }
    }

    public function delete() {
        $id = $this->getId();
        if ($this->adviser->find($id)) {
            if ($this->db->delete('adviser', array('id' => $id))) {
                $this->session->set_flashdata('notice', '删除成功！');
            }else{
                $this->session->set_flashdata('error', '修改失败，数据库错误！');
            }
            redirect(base_url().'admin.php?c=adviser&m=index');
        } else {
            show_404();
        }
    }

    private function getPhotos() {
        $photos[0] = '>> select <<';
        $query = $this->db->get('photo');
        foreach ($query->result() as $photo) {
            $photos[$photo->id] = $photo->label;
        }
        return $photos;
    }

    private function getId() {
        return intval($this->input->get('id'));
    }

}

